package cn.wxq.daily.empdailymgforserve.controller;

import cn.wxq.daily.empdailymgforserve.domain.Attendance;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.service.AttendanceService;
import com.alibaba.druid.sql.ast.statement.SQLIfStatement;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/attendance")
@Api(value = "AttendanceController|行政管理控制器")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

    /**
     * 条件查询
     *
     * @param attendance
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ApiOperation(value = "按条件查询")
    public Result findByPage(Attendance attendance) {
        List<Attendance> attendances = attendanceService.selectByPage(attendance);
        if (attendances != null) {
            return new Result(true, StatusCode.OK, "获取成功", attendances);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * 新增
     *
     * @param attendance
     * @return
     */
    @RequestMapping(value = "/insertAttendance", method = RequestMethod.POST)
    @ApiOperation(value = "打卡")
    public Result insertAttendance(Attendance attendance) {
        int row = attendanceService.insertAttendance(attendance);
        /**
         * 返回状态设定，1：上班打卡成功，2：下班打卡成功
         */
        if (row == 1) {
            return new Result(true, StatusCode.OK, "上班打卡成功");
        } else if (row == 2){
            return new Result(true, StatusCode.OK, "下班打卡成功");

        } else {
            return new Result(false, StatusCode.ERROR, "打卡失败");
        }
    }

    /**
     * 修改
     *
     * @param attendance
     * @return
     */
    @RequestMapping(value = "/updateAttendance", method = RequestMethod.POST)
    @ApiOperation(value = "修改")
    public Result updateAttendance(Attendance attendance) {
        int up = attendanceService.updateAttendance(attendance);
        if (up >0) {
            return new Result(true, StatusCode.OK, "修改成功");
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * 删除
     *
     * @param clockId
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "删除")
    public Result deleteById(Integer clockId) {
        int de = attendanceService.deleteById(clockId);
        if ( de >0) {
            return new Result(true, StatusCode.OK, "成功");
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }

    /**
     * id 获取详情
     *
     * @param clockId
     * @return
     */
    @RequestMapping(value = "/selectAttendanceById", method = RequestMethod.POST)
    @ApiOperation(value = "id 获取详情")
    public Result selectAttendanceById(Integer clockId) {
        Attendance info = attendanceService.selectAttendanceById(clockId);
        if (info != null) {
            return new Result(true, StatusCode.OK, "获取成功", info);
        } else {
            return new Result(false, StatusCode.ERROR, "返回数据异常");
        }
    }
}
